
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse, glue)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Load the synth did estimates
load(file = file.path(root, "tmp/allsynth.RData"), verbose = TRUE)

# Set up the matrix for storing output
output = matrix(nrow=6, ncol=8)
estimators = c("vs_did", "vs_time_wts", "vs_county_wts", "vs_sdid",
  "to_did", "to_time_wts", "to_county_wts", "to_sdid")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(output) = estimators
rownames(output) = output_components

# Store the estimated effects and standard errors
output[1:2,] = cbind(allsynth_dem$summary_table[,c("DID", "Time Weighted DID", 
      "DIFP (Regularized)", "Synthetic DID (SDID)")],
  allsynth_tur$summary_table[,c("DID", "Time Weighted DID", 
      "DIFP (Regularized)", "Synthetic DID (SDID)")])

# Count the number of years and units
c = 1
for(all_ests in list(allsynth_dem, allsynth_tur)){
  for(e_col in c("DID", "Time Weighted DID", 
      "DIFP (Regularized)", "Synthetic DID (SDID)")){
    est_setup = attr(all_ests$ests[[e_col]], "setup")
    output[3,c] = est_setup$T0 + 1
    output[4,c] = nrow(est_setup$Y) - est_setup$N0
    output[5,c] = nrow(est_setup$Y)
    output[6,c] = nrow(est_setup$Y)*ncol(est_setup$Y)
    c = c + 1
  }
}

# Put the main estimates in a tex table
cat(
"\\begin{table}[t]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in 2020.}",
"\\label{tab:main}}",
"\\begin{tabular}{lcccc|cccc}",
"\\toprule \\toprule",
" & \\multicolumn{4}{c}{Dem Vote Share (\\%)} & \\multicolumn{4}{c}{Turnout (\\%)} \\\\",
" & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(output[1,]), " \\\\"),
paste0(prep_table_rows(output[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(output[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(output[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(output[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"County Weights & No & No & Yes & Yes & No & No & Yes & Yes \\\\",
"Year Weights & No & Yes & No & Yes & No & Yes & No & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{9}{p{1\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/main_effects_table.tex"), sep="\n")

